package cn.houserent.service;

import cn.houserent.dao.HibernateUtil;
import cn.houserent.dao.UsersDao;
import cn.houserent.pojo.Users;
import org.hibernate.HibernateException;
import org.hibernate.Transaction;

import java.util.Iterator;
import java.util.List;

public class UserBiz {

    private UsersDao userDao = new UsersDao();

    public UsersDao getUserDao() {
        return userDao;
    }

    public void setUserDao(UsersDao userDao) {
        this.userDao = userDao;
    }

    //通过userDao.get()方法加载数据
    public Users findUserById(Integer id){
        Transaction tx = null;
        Users user = new Users();

        try {
            tx = HibernateUtil.currentSession().beginTransaction();     //开启事务
            user =userDao.get(id);
            tx.commit();
        } catch (HibernateException ex) {
            ex.printStackTrace();
            if (tx != null) {
                tx.rollback();      //回滚事务
            }
        }
        return user;
    }


    /**
     *  新增用户信息
     * @param users
     */
    public void addNewUser(Users users) {
        Transaction tx = null;
        try {
            tx = userDao.currentSession().beginTransaction();       //开启事务
            userDao.userSave(users);
            tx.commit();
        } catch (HibernateException ex) {
            ex.printStackTrace();
            if (tx != null) {
                tx.rollback();
            }
        }
    }


    public void deleteUser(Integer id) {
        Transaction tx = null;
        try {
            tx = userDao.currentSession().beginTransaction();       //开启事务
            //通过get()或load()方法加载要删除的部门对象
            Users deleteUser = userDao.load(id);
            userDao.userDelete(deleteUser);
            tx.commit();        //提交事务
        } catch (HibernateException ex) {
            ex.printStackTrace();
            if (tx != null) {
                tx.rollback();
            }
        }
    }

    public void updateUser(Users users) {
        Transaction tx = null;
        try {
            tx = userDao.currentSession().beginTransaction();       //开启事务
            Users usersToUpdate = userDao.load(users.getId());
            usersToUpdate.setName(users.getName());
            usersToUpdate.setTelephone(users.getTelephone());
            usersToUpdate.setPassword(users.getPassword());
            usersToUpdate.setIsadmin(users.getIsadmin());
            usersToUpdate.setUsername(users.getUsername());
            tx.commit();
        } catch (HibernateException ex) {
            ex.printStackTrace();
            if (tx != null) {
                tx.rollback();
            }
        }
    }

    public Users mergeUsers(Users users) {
        Transaction tx = null;
        Users persistentUsers = null;
        try {
            tx = userDao.currentSession().beginTransaction();       //开启事务
            persistentUsers = userDao.merge(users);
            tx.commit();
        } catch (HibernateException ex) {
            ex.printStackTrace();
            if (tx != null) {
                tx.rollback();
            }
        }
        return persistentUsers;
    }


    public void saveOrUpdate(Users users){
        Transaction tx = null;
        try {
            tx = userDao.currentSession().beginTransaction();
            userDao.saveOrUpdate(users);
            tx.commit();
        } catch (HibernateException ex) {
            ex.printStackTrace();
            if (tx != null) {
                tx.rollback();
            }
        }
    }


    public List<Users> findAllUsers() {
        Transaction tx = null;
        List<Users> list = null;
        try {
            tx = userDao.currentSession().beginTransaction();       //开启事务
            list = userDao.findAllUsers();      //持久化操作
            tx.commit();        //提交事务
        } catch (HibernateException ex) {
            ex.printStackTrace();
            if (tx != null) {
                tx.rollback();      //关闭事务
            }
        }
        return list;
    }


    public Iterator<Users> findUsersByName(String name) {
        Transaction tx = null;
        Iterator<Users> userList = null;
        try {
            tx = userDao.currentSession().beginTransaction();   //开启事务
            userList = userDao.findUsersByName(name);
            Users users = null;
            while (userList.hasNext()) {
                users = userList.next();
                System.out.println("id：" + users.getId() + "\tname：" + users.getName());
            }
            tx.commit();        //提交事务
        } catch (HibernateException ex) {
            ex.printStackTrace();
            if (tx != null) {
                tx.rollback();
            }
        }
        return userList;
    }


    public List<Users> findByUsersByPage(int pageNo,int pageSize) {
        Transaction tx = null;
        List<Users> usersList = null;
        try {
            tx = userDao.currentSession().beginTransaction();       //开启事务
            usersList = userDao.findHousesByPage(pageNo,pageSize);
            tx.commit();        //提交事务
        } catch (HibernateException ex) {
            ex.printStackTrace();
            if (tx != null) {
                tx.rollback();      //回滚事务
            }
        }
        return usersList;
    }


    public List<Users> loginUser(String name,String password) {

        Transaction tx = null;
        List<Users> usersList = null;
        try {
            tx = userDao.currentSession().beginTransaction();       //开启事务
            usersList = userDao.loginUser(name,password);
            tx.commit();    //提交事务
        } catch (HibernateException ex) {
            ex.printStackTrace();
            if (tx != null) {
                tx.rollback();
            }
        }
        return usersList;
    }
}